﻿#region Using

using Common.Enums;

using GameCommon;
using GameCommon.Manager;
using GameCommon.ManagerInterfaces;

using GameNetwork;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

#endregion

namespace FarmersBreakfast
{
    public class Game1 : Game
    {
        private readonly GraphicsDeviceManager graphics;

        private bool firstUpdate;

        private IKeyboardManager keyboardManager;

        private IMouseManager mouseManager;

        private IRenderManager renderManager;

        private IScreenManager screenManager;

        private SpriteBatch spriteBatch;

        public Game1()
        {
            this.graphics = new GraphicsDeviceManager(this);

            this.graphics.IsFullScreen = false;
            this.firstUpdate = true;
            this.IsMouseVisible = true;

            this.graphics.PreferredBackBufferWidth = 1280;
            this.graphics.PreferredBackBufferHeight = 720;
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            this.GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            this.renderManager.Draw(gameTime);

            base.Draw(gameTime);
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            this.spriteBatch = new SpriteBatch(this.GraphicsDevice);

            GameCommonRegistry.Register();
            GameClientRegistry.Register();
            GameCommonRegistry.Register(this.Content, this.GraphicsDevice, this.spriteBatch);

            this.renderManager = Managers.RenderManager;
            this.screenManager = new ScreenManager();
            this.keyboardManager = Managers.KeyboardManager;
            this.mouseManager = Managers.MouseManager;

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            ////if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            ////{
            ////    this.Exit();
            ////}
            base.Update(gameTime);

            if (this.firstUpdate)
            {
                this.screenManager.AddScreen(ScreenEnum.ConnectionScreen);
                this.firstUpdate = false;
                return;
            }

            this.keyboardManager.Update(gameTime);
            this.mouseManager.Update(gameTime);
            this.renderManager.Update(gameTime);
        }
    }
}